<?php

class main extends controllers{

    public $js;
    public $css;
    public $_config;

    public function __construct(){
        parent::__construct();
        //print_r($this);exit;
        define('BASEURI','/');    // 网站根目录
        $this->loadModel('g5_admin_acl');

        if($_GET[0]!=='iap'){
	        if( ! $this->is_login()){
	            redirect(SITE_URL.'login/index/?referer='.$_SERVER['QUERY_URI']);
	        }
	        if( ! $this->g5_admin_acl->authUser() ){
	            $this->report($this->systemLang['userGroupNotAuth'],BASEURI.'admin/main');
	        }
        }
        $this->cache = $GLOBALS['memcached']; // 缓存

        // 是否删除
        $delConfirm=$this->systemLang['delConfirm'];
        define("IS_DELETE","onclick=\"if(!confirm('$delConfirm')){return false;}\"");
    }

    /**
     * 名称
     * @return void
     * @param args array 请求参数
     * @param act string  请求方法
     */
    protected function soap($args, $act = ''){
        $soap_uri  = 'http://'.$_SERVER["HTTP_HOST"].'/web/index.php';
        $soap_host = PASSPORT_URL;
        $token     = create_token($args);
        $client    = new SoapClient(null, array('location' => $soap_host.'soap.php','uri' => $soap_uri));
        if ( empty($act) ){
            return array(0,$this->systemLang['missMethod']);
        }try {
            $rs = $client->get($act,$args,$token);
            return $rs;
        } catch (SoapFault $e) {
            die('SOAP Error: '.$e->getMessage());
        }
        exit;
    }


    /*
    * 验证是否登录
    **/
    function is_login() {
        if( ! empty($_SESSION['adminmanager']) ){
            return true;
        }
        if( isset($_COOKIE['USEID']) && isset($_COOKIE['KEEPLIVE']) ){
            $user = $this->g5_user->checkUser(array('user_name'=>$_COOKIE['USEID'],'user_pwd'=>$_COOKIE['KEEPLIVE']));
            if($user['user_pwd'] === $_COOKIE['KEEPLIVE']){
                $userData = $this->g5_user->getUser($user['user_id']); // 更新SESSION信息
                $_SESSION['adminmanager'] = $userData;
                return true;
            }
        }
        return false;
    }

    /*
    * 跳转页面
    * @param message String 跳转提示信息
    * @param url   String  Default / 目标URL
    * @param auto_refresh bool 自动跳转
    **/
    public function report($message = '', $url = '', $auto_refresh = true){
        //查看cookie返回页面
        if( ! empty($_COOKIE['referer'])){
            $referer = $_COOKIE['referer'];
            unset($_COOKIE['referer']);
            setcookie('referer', '', -86400);
            $url = empty($url) ? $referer : $url;
        }
        if(is_bool($message)){
            $message = $message ? $this->systemLang['operateSuccess'] : $this->systemLang['operateFailed'];
        }
        $this->message = $message;
        $this->url = empty($url) ? SITE_URL : $url;
        $this->auto_refresh = $auto_refresh;
        $this->display('redirect');
    }

     /*
     * 记录管理日志
     * @param description String 操作描述
     **/
     function adminlog($description = '') {
        $this->loadModel('g5_admin_log');
        $data['user_id']        = $_SESSION['adminmanager']['user_id'];
        $data['user_true_name'] = $_SESSION['adminmanager']['user_true_name'];
        $data['query_uri']      = ltrim($_SERVER['REQUEST_URI'], '/');
        $data['time']           = time();
        $data['description']    = $description;
        $data['client_ip'] = getClientIP();
        $this->g5_admin_log->insert($data);
    }

    /*
    *   分页
    * @param page  Integer 当前页数
    * @param total Integer 总记录数
    * @param urlParam String URL额外参数
    * @param pageSize Integer 每页记录数
    * return 分页HTML内容
    **/
    function page($page = 1, $total = 10, $params = NULL, $pageSize = 10) {
        require_once (VENDORS_PATH . 'page'.DS.'pager.php');
        require_once (VENDORS_PATH . 'page'.DS.'drivers'.DS.'pager_ggwhm_driver.php');
        $p = new pager_ggwhm_driver($this->systemLang);
        $p->currentPage = $page;
        $p->size  = $pageSize;
        $p->total = $total;
        $p->urlParam = $params;
        $this->page = $p;
    }


    public function __destruct(){
        parent::__destruct();
    }
	/**
	 * 游戏权限
	 */
	public function gameCheck($game_id){
    	$sess_games = $_SESSION['adminmanager']['user_games'];
    	$games_array = array();
    	if(!empty($sess_games)){
    		$games_array = explode(',',$sess_games);
    	}
		if($game_id>0 && in_array($game_id,$games_array)){
			return true;
		}else{
			$this->report('无游戏权限','/');
		}
	}
}
?>